using System;
using System.Collections.Generic;
using System.Net;
using Mono.Cecil;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Xml;
using staruml_baikentool_csharpreverse.Model;


namespace staruml_baikentool_csharpreverse
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string dll = args[0];

            AssemblyDefinition sourceAssembly = AssemblyFactory.GetAssembly(dll);

            ReverseEngineeringResult result = new ReverseEngineeringResult();
            result.classes = new List<ClassEntity>();

            foreach (ModuleDefinition module in sourceAssembly.Modules)
            {

                foreach (TypeDefinition type in module.Types)
                {
                    ClassEntity classEntity = new ClassEntity();
                    result.classes.Add(classEntity);

                    classEntity.name = type.FullName;
                    classEntity.baseType = type.DeclaringType == null ? "" : type.DeclaringType.FullName;

                    classEntity.interfaces = "";
                    foreach (TypeReference typeRef in type.Interfaces)
                    {
                        classEntity.interfaces += "," + (typeRef.DeclaringType == null ? "" : typeRef.DeclaringType.FullName);
                    }
                    classEntity.interfaces = classEntity.interfaces.StartsWith(",") ? classEntity.interfaces.Substring(1) : classEntity.interfaces;

                    classEntity.attributes = type.Attributes.ToString();

                    classEntity.methods = new List<MethodEntity>();
                    classEntity.fields = new List<FieldEntity>();
                    classEntity.properties = new List<PropertyEntity>();

                    foreach (PropertyDefinition property in type.Properties)
                    {
                        PropertyEntity propertyEntity = new PropertyEntity();
                        classEntity.properties.Add(propertyEntity);

                        propertyEntity.name = property.Name;
                        propertyEntity.attributes = property.Attributes.ToString();
                        propertyEntity.type = property.DeclaringType.FullName;
                    }

                    foreach (MethodDefinition method in type.Methods)
                    {
                        if (!method.IsSpecialName)
                        {
                            MethodEntity methodEntity = new MethodEntity();
                            classEntity.methods.Add(methodEntity);

                            methodEntity.name = method.Name;
                            methodEntity.attributes = method.Attributes.ToString();
                            methodEntity.type = method.ReturnType.ReturnType.Name;

                            methodEntity.parameters = "";
                            foreach (ParameterDefinition parameter in method.Parameters)
                            {
                                methodEntity.parameters += "," + parameter.Name + ":" + (parameter.ParameterType == null ? "" : parameter.ParameterType.Name);
                            }
                            methodEntity.parameters = methodEntity.parameters.StartsWith(",") ? methodEntity.parameters.Substring(1) : methodEntity.parameters;
                        }
                    }

                    foreach (FieldDefinition field in type.Fields)
                    {
                        FieldEntity fieldEntity = new FieldEntity();
                        classEntity.fields.Add(fieldEntity);

                        fieldEntity.name = field.Name;
                        fieldEntity.type = field.DeclaringType.FullName;
                        fieldEntity.attributes = field.Attributes.ToString();
                    }




                }
            }


            XmlSerializer serializer = new XmlSerializer(typeof(ReverseEngineeringResult));

            XmlTextWriter memoryStream = new XmlTextWriter(Console.OpenStandardOutput(), null);




            serializer.Serialize(memoryStream, result);



            memoryStream.Close();

            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            //request.Method = "POST";

            //request.ContentType = "application/x-www-form-urlencoded";
            //// Set the ContentLength property of the WebRequest.
            //request.ContentLength = memoryStream.Length;
            //// Get the request stream.
            //Stream dataStream = request.GetRequestStream();
            //// Write the data to the request stream.

            //int bufferSize = 1024;
            //byte[] data=new byte[bufferSize];
            //int position=0;
            //while ((position = memoryStream.Read(data, position, position + bufferSize <= (int)memoryStream.Length ? bufferSize : (int)memoryStream.Length - position)) != -1)
            //{
            //    dataStream.Write(data,0,bufferSize);
            //}

            //// Close the Stream object.
            //dataStream.Close();
            //// Get the response.
            //WebResponse response = request.GetResponse();

            Environment.Exit(0);
        }
    }
}